﻿
namespace LS.Nc6.Application.Cms
{
    /// <summary>
    /// 栏目表
    /// </summary>
    public class CmsChannelService : BaseService<CmsChannelEntity>, ICmsChannelService, ITransient
    {
        public async Task<int> GetChildrenCount(long id)
        {
            return await DbContext.Instance.Queryable<CmsChannelEntity>().Where(a => a.ParentId == id).CountAsync();
        }

        public async Task<List<string>> GetIndexNames()
        {
            return await DbContext.Instance.Queryable<CmsChannelEntity>().Select(a => a.IndexName).ToListAsync();
        }

        public async Task<int> GetMaxTaxis(long parentId)
        {
            var   maxtaxis = await DbContext.Instance.Queryable<CmsChannelEntity>().Where(a => a.ParentId == parentId).MaxAsync(a => a.Taxis);
            return maxtaxis!=null? (int)maxtaxis:0;
        }

        public async Task<int> UpdateChildrenCount(long id)
        {
            var childrenCount = await GetChildrenCount(id);
            return await DbContext.Instance.Updateable<CmsChannelEntity>().SetColumns(a => a.ChildrenCount == childrenCount).Where(a => a.Id == id).ExecuteCommandAsync();
        }

        public async Task<int> UpdateGroupName(string ids, string groupNames)
        {
            return await DbContext.Instance.Updateable<CmsChannelEntity>().SetColumns(a => a.GroupNames == groupNames).Where(a => ids.GetStringToLongArryNoNull().Contains(a.Id)).ExecuteCommandAsync();
        }

        public async Task<int> SetTaxis(long channelId, int num)
        {
            //return await DbContext.Instance.Updateable<CmsChannelEntity>().SetColumns(a => a.Taxis == a.Taxis- num).Where(a => ids.GetStringToLongArryNoNull().Contains(a.Id)).ExecuteCommandAsync();
            return await DbContext.Instance.Updateable<CmsChannelEntity>().SetColumns(a => a.Taxis == num).Where(a=>a.Id == channelId).ExecuteCommandAsync();   
        }
        public async Task<int> UpdateTaxis(long parentId, long channelId, int isUp)
        {
            var channelIds = DbContext.Instance.Queryable<CmsChannelEntity>().Where(a=>a.ParentId== parentId).Select(a=>a.Id).ToList();
            var index = channelIds.IndexOf(channelId);

            if (isUp == 1 && index == 0) return 0;
            if (isUp==0 && index == channelIds.Count - 1) return 0;
            if (isUp==1)
            {
                channelIds.Remove(channelId);
                channelIds.Insert(index - 1, channelId);
            }
            else
            {
                channelIds.Remove(channelId);
                channelIds.Insert(index + 1, channelId);
            }
            for (var taxis = 1; taxis <= channelIds.Count; taxis++)
            {
                await SetTaxis(channelIds[taxis - 1], taxis);
            }
            return 1;
        }
        public async Task<bool> IsAncestorOrSelfAsync(long parentId, long childId)
        {
            if (parentId == childId)
            {
                return true;
            }
            var channel = await GetByIdAsync(childId);
            if (channel == null)
            {
                return false;
            }
            if (CommonUtil.Contains(CommonUtil.GetLongList(channel.ParentsPath), parentId))
            {
                return true;
            }
            return false;
        }

        public async  Task<List<long>> GetChannelIdsAsync(long channelId, int scopeType)
        {
            var channelIds = new List<long>();

            if (channelId == 0) return channelIds;

            if (scopeType == 0)
            {
                channelIds = new List<long> { channelId };
            }
            else if (scopeType == 1)
            {
                channelIds =await GetChildIds(channelId);
                channelIds.Add(channelId);
            }
            else if (scopeType == 2)
            {
                channelIds =await GetChildIds(channelId);
            }
            else if (scopeType == 3)
            {
                var summaries = await DbContext.Instance.Queryable< CmsChannelEntity >().ToListAsync();
                GetChildIdsRecursive(summaries, channelIds, channelId);
            }
            return channelIds;
        }


        #region 私有方法
        private void GetChildIdsRecursive(List<CmsChannelEntity> summaries, List<long> list, long parentId)
        {
            var childIds = summaries.Where(x => x.ParentId == parentId).Select(x => x.Id).ToList();
            if (childIds.Count > 0)
            {
                list.AddRange(childIds);
                foreach (var childId in childIds)
                {
                    GetChildIdsRecursive(summaries, list, childId);
                }
            }
        }
        private async Task<List<long>> GetChildIds(long parentId)
        {
            return await DbContext.Instance.Queryable<CmsChannelEntity>().Where(x => x.ParentId == parentId).Select(x => x.Id).ToListAsync();
        }
        #endregion
    }
}
